package com.dzf.service.impl;

import com.dzf.entity.BookType;
import com.dzf.entity.Type;
import com.dzf.mapper.BookTypeMapper;
import com.dzf.service.BookTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BookTypeServiceImpl implements BookTypeService {
    @Autowired
    BookTypeMapper bookTypeMapper;

    @Override
    public List<BookType> findAll() {
        return bookTypeMapper.findAll();
    }

    @Override
    public BookType findById(int id) {
        return bookTypeMapper.findById(id);
    }

    @Override
    public List<Type> findAll2() {
        Map<Integer,Type> map = new HashMap<>();
        Map<Integer,Type> map2 = new HashMap<>();
        List<Type> types = new ArrayList<>();

        List<BookType> list = bookTypeMapper.findAll2();
        List<BookType> bookTypes = new ArrayList<>();
        List<Integer> keys = new ArrayList<>();

        int i = 1;
        int k = 0;
        int flag = 0;
        for (BookType bookType : list) {
            if(bookType.getPid() == 0){
                for (BookType type : list) {
                    if(bookType.getId() == type.getPid()){
                        flag = 1;
                    }
                }
                if (flag == 0){
                    Type type = new Type();
                    type.setId(bookType.getId());
                    type.setPid(bookType.getPid());
                    type.setIsdel(bookType.getIsdel());
                    type.setPrio(bookType.getPrio());
                    type.setType(bookType.getType());
                    type.setList(new ArrayList());
                    types.add(type);
                }
                else{
                    Type type = new Type();
                    type.setId(bookType.getId());
                    type.setPid(bookType.getPid());
                    type.setIsdel(bookType.getIsdel());
                    type.setPrio(bookType.getPrio());
                    type.setType(bookType.getType());
                    type.setList(bookTypes);
                    map.put(bookType.getId(),type);
                    System.out.println("1");
                }
                flag = 0;
            }else{
                for(int key : map.keySet()){
                    System.out.println(key);
                    keys.add(key);
                }
                for(int key : map.keySet()){
                    k = keys.get(i);

                    if(bookType.getPid() == k){
                        System.out.println("111");
                        bookTypes = new ArrayList<>();
                        i++;
                    }
                    if(bookType.getPid() == key){
                        bookTypes.add(bookType);
                        map.get(key).setList(bookTypes);
                    }

                }
            }

        }
        for(int key : map.keySet()){
            types.add(map.get(key));
        }
        for (Type type : types) {
            System.out.println(type);
        }

        return types;
    }

    @Override
    public void amend(BookType bookType) {
        if (bookType.getId()==0)
        {
            bookTypeMapper.bookTypeAdd(bookType);
            System.out.println("tianjia");
        }else{
            bookTypeMapper.bookTypeUpd(bookType);
            System.out.println("修改");
        }
    }


}
